﻿using BootstrapBlazor.OnlyServerZhongShun.Data.dbmodels;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using SqlSugar;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;

namespace BootstrapBlazor.OnlyServerZhongShun.auth
{
    public interface ICustomAuthenticationService
    {
        Task<bool> LoginAsync(string username, string password);
        Task LogoutAsync();
    }

    public class CustomAuthenticationService : ICustomAuthenticationService
    {
        // 这里可以注入您的用户数据存储，如数据库或其他服务
        public SqlSugarScope _SqlSugarScope { get; set; }
        public CustomAuthenticationStateProvider _customAuthenticationStateProvider { get; set; }
        public CustomAuthenticationService(SqlSugarScope sqlSugarScope, CustomAuthenticationStateProvider customAuthenticationStateProvider)
        {
            _SqlSugarScope = sqlSugarScope;
            _customAuthenticationStateProvider = customAuthenticationStateProvider;
        }
        public async Task<bool> LoginAsync(string username, string password)
        {
            // 这里实现您的登录逻辑
            // 比如检查用户名和密码是否匹配
            // 返回 true 表示登录成功，返回 false 表示登录失败
            Sys_User User = await _SqlSugarScope.Queryable<Sys_User>()
                                            .Where(it => it.UserName == username && it.UserPwd == EncryptDES(password, "C5ABA9E202D94C43A3CA66002BF77FAF"))
                                            .FirstAsync();

            if (User != null )
            {
                _customAuthenticationStateProvider.MarkUserAsAuthenticated(username);
                return true;
            }
            else
            {
                return false;
            }
        }

        public async Task LogoutAsync()
        {
            // 实现登出逻辑
        }

        public string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 16));
                byte[] rgbIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

                using (var DCSP = Aes.Create())
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(inputByteArray, 0, inputByteArray.Length);
                            cStream.FlushFinalBlock();
                            return Convert.ToBase64String(mStream.ToArray()).Replace('+', '_').Replace('/', '~');
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("密码加密异常" + ex.Message);
            }

        }
    }
}
